<?php  if ( ! defined('BASE_ROOT')) exit('No direct script access allowed');
/**
 * Strip Slashes
 *
 * Removes slashes contained in a string or in an array
 *
 * @access	public
 * @param	mixed	string or array
 * @return	mixed	string or array
 */	
if ( ! function_exists('strip_slashes'))
{
	function strip_slashes($str)
	{
		if (is_array($str))
		{	
			foreach ($str as $key => $val)
			{
				$str[$key] = strip_slashes($val);
			}
		}
		else
		{
			$str = stripslashes($str);
		}
	
		return $str;
	}
}

// ------------------------------------------------------------------------

/**
 * Strip Quotes
 *
 * Removes single and double quotes from a string
 *
 * @access	public
 * @param	string
 * @return	string
 */	
if ( ! function_exists('strip_quotes'))
{
	function strip_quotes($str)
	{
		return str_replace(array('"', "'"), '', $str);
	}
}

// ------------------------------------------------------------------------

/**
 * Quotes to Entities
 *
 * Converts single and double quotes to entities
 *
 * @access	public
 * @param	string
 * @return	string
 */	
if ( ! function_exists('quotes_to_entities'))
{
	function quotes_to_entities($str)
	{	
		return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);
	}
}

// ------------------------------------------------------------------------
/**
 * Reduce Double Slashes
 *
 * Converts double slashes in a string to a single slash,
 * except those found in http://
 *
 * http://www.some-site.com//index.php
 *
 * becomes:
 *
 * http://www.some-site.com/index.php
 *
 * @access	public
 * @param	string
 * @return	string
 */	
if ( ! function_exists('reduce_double_slashes'))
{
	function reduce_double_slashes($str)
	{
		return preg_replace("#([^:])//+#", "\\1/", $str);
	}
}
	
// ------------------------------------------------------------------------

/**
 * Reduce Multiples
 *
 * Reduces multiple instances of a particular character.  Example:
 *
 * Fred, Bill,, Joe, Jimmy
 *
 * becomes:
 *
 * Fred, Bill, Joe, Jimmy
 *
 * @access	public
 * @param	string
 * @param	string	the character you wish to reduce
 * @param	bool	TRUE/FALSE - whether to trim the character from the beginning/end
 * @return	string
 */	
if ( ! function_exists('reduce_multiples'))
{
	function reduce_multiples($str, $character = ',', $trim = FALSE)
	{
		$str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);

		if ($trim === TRUE)
		{
			$str = trim($str, $character);
		}

		return $str;
	}
}
	
// ------------------------------------------------------------------------

/**
 * Create a Random String
 *
 * Useful for generating passwords or hashes.
 *
 * @access	public
 * @param	string 	type of random string.  Options: alunum, numeric, nozero, unique
 * @param	integer	number of characters
 * @return	string
 */
if ( ! function_exists('random_string'))
{	
	function random_string($type = 'alnum', $len = 8)
	{					
		switch($type)
		{
			case 'alnum'	:
			case 'numeric'	:
			case 'nozero'	:
		
					switch ($type)
					{
						case 'alnum'	:	$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
							break;
						case 'numeric'	:	$pool = '0123456789';
							break;
						case 'nozero'	:	$pool = '123456789';
							break;
					}

					$str = '';
					for ($i=0; $i < $len; $i++)
					{
						$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
					}
					return $str;
			  break;
			case 'unique' : return md5(uniqid(mt_rand()));
			  break;
		}
	}
}

// ------------------------------------------------------------------------

/**
 * Alternator
 *
 * Allows strings to be alternated.  See docs...
 *
 * @access	public
 * @param	string (as many parameters as needed)
 * @return	string
 */	
if ( ! function_exists('alternator'))
{
	function alternator()
	{
		static $i;	

		if (func_num_args() == 0)
		{
			$i = 0;
			return '';
		}
		$args = func_get_args();
		return $args[($i++ % count($args))];
	}
}

// ------------------------------------------------------------------------

/**
 * Repeater function
 *
 * @access	public
 * @param	string
 * @param	integer	number of repeats
 * @return	string
 */	
if ( ! function_exists('repeater'))
{
	function repeater($data, $num = 1)
	{
		return (($num > 0) ? str_repeat($data, $num) : '');
	} 
}

if ( ! function_exists('html2txt'))
{
	function html2txt($document){
		$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
				   '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
				   '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
				   '@<![\s\S]*?--[ \t\n\r]*>@',        // Strip multi-line comments including CDATA
				   '@&nbsp;@'
				   );
				   $text = preg_replace($search, '', $document);
				   return $text;
	}
}

if ( ! function_exists('textarea_to_html'))
{
	function textarea_to_html($str){
		$str=str_replace(chr(13),'<br>',$str);
		$str=str_replace(chr(9),'&nbsp;&nbsp;',$str);
		$str=str_replace(chr(32),'&nbsp;',$str);
		return $str;
	}
}

if ( ! function_exists('html_to_textarea'))
{
	function html_to_textarea($str){
		$str=str_replace('<br>',chr(13),$str);
		$str=str_replace('&nbsp;',chr(32),$str);
		return $str;
	}
}

if ( ! function_exists('encrypt'))
{
	function encrypt($string,$action='ENCODE'){
		$action != 'ENCODE' && $string = base64_decode(str_replace(array('-','_'),array('+','/'),$string));
		$code = '';
		$key  = substr(md5($_SERVER['HTTP_USER_AGENT'].'caixudong'),8,18);
		$keylen = strlen($key);
		$strlen = strlen($string);
		for ($i=0;$i<$strlen;$i++) {
			$k	   = $i % $keylen;
			$code .= $string[$i] ^ $key[$k];
		}
		return ($action!='DECODE' ? str_replace(array('+','/','='),array('-','_',''),base64_encode($code)): $code);
	}
}

if ( ! function_exists('cutstr'))
{
	function cutstr($string,$length,$dot = '...'){
		if(strlen($string) <= $length){
			return $string;
		}
		$string = str_replace(array('&amp;','&quot;','&lt;','&gt;'), array('&','"','<','>'), $string);
		$strcut = '';
		$n = $tn = $noc = 0;
		while($n < strlen($string)){
			$t = ord($string[$n]);
			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)){
				$tn = 1; $n++; $noc++;
			}elseif(194 <= $t && $t <= 223){
				$tn = 2; $n += 2; $noc += 2;
			}elseif(224 <= $t && $t < 239) {
				$tn = 3; $n += 3; $noc += 2;
			}elseif(240 <= $t && $t <= 247){
				$tn = 4; $n += 4; $noc += 2;
			}elseif(248 <= $t && $t <= 251){
				$tn = 5; $n += 5; $noc += 2;
			}elseif($t == 252 || $t == 253){
				$tn = 6; $n += 6; $noc += 2;
			}else{
				$n++;
			}
			if($noc >= $length){
				break;
			}
		}
		if($noc > $length){
			$n -= $tn;
		}
		$strcut = substr($string, 0, $n);
		$strcut = str_replace(array('&','"','<','>'), array('&amp;','&quot;','&lt;','&gt;'), $strcut);
		return $strcut;
	}
}